package NiuKe;

import java.util.Scanner;

/**
 * 一个数组有 N 个元素，求连续子数组的最大和。
 * 例如：[-1,2,1]，和最大的连续子数组为[2,1]，其和为 3
 */
public class MaximumSum {
    public static void main(String[] args) {
//        Scanner in = new Scanner(System.in);
//        int n = in.nextInt();
//        int[] nums = new int[n];
//        for (int i = 0; i < n; i++) {
//            nums[i] = in.nextInt();
//        }
        int[] nums = {-1,2,1};
        int solution = solution(nums);
        System.out.println(solution);
    }
    public static int solution(int[] nums){
        int max = Integer.MIN_VALUE;
        int right = 0;
        int sum = 0;
        while (right < nums.length){
            if(right > 0 && sum < 0){
                sum = nums[right++];
                max = Math.max(max,sum);
                continue;
            }
            sum += nums[right];
            right++;
            max = Math.max(max,sum);
        }
        return max;
    }
}
